package com.wangxiang.shop.order.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangxiang.common.utils.*;
import com.wangxiang.shop.order.dao.MemberOrderDao;
import com.wangxiang.shop.order.dao.ReceiptOrderDao;
import com.wangxiang.shop.order.dao.ShipOrderDao;
import com.wangxiang.shop.order.entity.MemberOrderEntity;
import com.wangxiang.shop.order.entity.ReceiptOrderEntity;
import com.wangxiang.shop.order.entity.ShipOrderEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wangxiang.shop.order.dao.UnsubscribeOrderDao;
import com.wangxiang.shop.order.entity.UnsubscribeOrderEntity;
import com.wangxiang.shop.order.service.UnsubscribeOrderService;


@Service("unsubscribeOrderService")
public class UnsubscribeOrderServiceImpl extends ServiceImpl<UnsubscribeOrderDao, UnsubscribeOrderEntity> implements UnsubscribeOrderService {
    @Autowired
    private UnsubscribeOrderDao unsubscribeOrderDao;
    @Autowired
    private MemberOrderDao memberOrderDao;
    @Autowired
    private ShipOrderDao shipOrderDao;
    @Autowired
    private ReceiptOrderDao receiptOrderDao;

    @Override
    public PageEntity page(HashMap<String, Integer> map) {
        PageHelper.startPage(map.get("pageNum") , map.get("pageSize"));
        List<UnsubscribeOrderEntity> unsubscribeOrderEntities = unsubscribeOrderDao.selectList(null);
        unsubscribeOrderEntities.forEach((item)->{
            TimeUtils timeUtils = new TimeUtils();
            String s = timeUtils.timeChange(item.getTime());
            item.setStrtime(s);
        });
        PageInfo<UnsubscribeOrderEntity> unsubscribeOrderEntityPageInfo = new PageInfo<>(unsubscribeOrderEntities);
        PageGenerate pageGenerate = new PageGenerate();
        PageEntity generate = pageGenerate.generate(unsubscribeOrderEntityPageInfo);
        return generate;
    }

    @Override
    public List<HashMap<String, Object>> pageList(List<UnsubscribeOrderEntity> list) {
        ArrayList<HashMap<String, Object>> hashMaps = new ArrayList<>();
        list.forEach((item)->{
            HashMap<String, Object> map = new HashMap<>();
            MemberOrderEntity orderEntity = memberOrderDao.selectById(item.getOrderId());  //全表
            ShipOrderEntity shipEntity = shipOrderDao.selectByOrderId(item.getOrderId());  //发货日志表
            ReceiptOrderEntity receiptEntity = receiptOrderDao.selectByOrderId(item.getOrderId());
            System.out.println("发货"+shipEntity);
            System.out.println("收货"+receiptEntity);
            //其他接口调用数据
            //商城服务 spu,sku
            map.put("spuId",orderEntity.getSpuId());
            map.put("skuId",orderEntity.getSkuId());

            //会员模块
            map.put("userId",orderEntity.getUserId());
            map.put("receiptId",orderEntity.getReceiptId());

            //系统模块
            map.put("doUserId",item.getDoUserId());

            //公共
            map.put("num",orderEntity.getNum());
            map.put("price",orderEntity.getPrice());
            map.put("status",item.getStatus());
            map.put("remark",item.getRemark());
            map.put("userRemark",item.getApplyBc());
            map.put("rpUserRemark",item.getRpRemark());
            map.put("orderId",item.getOrderId());

            //时间 下单 发货 收货 退订
            TimeUtils timeUtils = new TimeUtils();
            String orderTime = timeUtils.timeChange(orderEntity.getTime());
            map.put("orderTime",orderTime);
            if (shipEntity!=null){

                String shipTime = timeUtils.timeChange(shipEntity.getTime());
                map.put("shipTime",shipTime);

            }
            if (receiptEntity!=null){

                String receiptTime = timeUtils.timeChange(receiptEntity.getTime());
                map.put("receiptTime",receiptTime);
            }

            map.put("unsubscribeTime",item.getStrtime());
            hashMaps.add(map);



        });
        return hashMaps;
    }
}